/**
 * 
 */
package ro.utcn.tp.taskManagement.gui;

import java.awt.Container;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;

import ro.utcn.tp.taskManagement.business.ImageService;
import ro.utcn.tp.taskManagement.business.TasksManager;
import ro.utcn.tp.taskManagement.model.ImageDefs;
import ro.utcn.tp.taskManagement.model.ImagePanel;
import ro.utcn.tp.taskManagement.model.Task;
import ro.utcn.tp.taskManagement.model.User;

/**
 * @author Coni
 *
 */
public class SearchPage implements ActionListener{

	public static final int SEARCH_BY_NAME = 0;
	public static final int SEARCH_BY_QUANTITY = 1;

	private JLabel jlbTitlu;
	private JLabel jlbDummy;
	private JRadioButton jrbSearchByDescription;
	private JRadioButton jrbStatus;

	private JLabel jlbSearchByDescription;
	private JLabel jlbStatus;
	private JTextField jtfSearchByDescription;
	
	private JButton jbnCancel;
	private JButton jbnSearch;
	
	private JComboBox<String> jcbStatus;
	private Container container;

	/** A reference to the image service. */

	private ImageService imageService;

	private static SearchPage instance;
	private User user;
	private List<Task> tasks;


	/**
	 * The constructor of the OrderProductPage class. initializes the screen.
	 */
	public SearchPage() {

		imageService = ImageService.getInstance();
		createGUI();

	}
	public SearchPage(User user) {
		this.user=user;
		imageService = ImageService.getInstance();
		createGUI();

	}

	public void createGUI() {

		container = new ImagePanel(
				imageService.getImage(ImageDefs.APPLICATION_BACKGROUND_PATH));
		container.setLayout(new GridBagLayout());
		arrangeComponents();
	}

	public void arrangeComponents() {

		jlbTitlu = new JLabel("Search tasks");
		jlbDummy = new JLabel("    ");
		jlbStatus= new JLabel("Status:");
		jrbSearchByDescription = new JRadioButton("Search by description:");
		jrbStatus = new JRadioButton("Search by status:");
	
		jlbSearchByDescription= new JLabel("Search for:");
		jtfSearchByDescription = new JTextField("");
		jtfSearchByDescription.setEditable(true);

        jcbStatus = new JComboBox<String>();
		fillStatusComboBox();
		jbnSearch = new JButton("Search");
		jbnCancel = new JButton("Cancel");

		Font font = new Font("Times New Roman", Font.ITALIC, 30);
		jlbTitlu.setFont(font);

		ButtonGroup searchGroup = new ButtonGroup();
		searchGroup.add(jrbSearchByDescription);
		searchGroup.add(jrbStatus);

		GridBagConstraints gridBagConstraintsx01 = new GridBagConstraints();
		gridBagConstraintsx01.gridx = 0;
		gridBagConstraintsx01.gridy = 0;
		gridBagConstraintsx01.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx01.gridwidth = 3;
		gridBagConstraintsx01.fill = GridBagConstraints.BOTH;
		container.add(jlbTitlu, gridBagConstraintsx01);

		GridBagConstraints gridBagConstraintsx02 = new GridBagConstraints();
		gridBagConstraintsx02.gridx = 0;
		gridBagConstraintsx02.gridy = 1;
		gridBagConstraintsx02.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx02.gridwidth = 3;
		gridBagConstraintsx02.fill = GridBagConstraints.BOTH;
		container.add(jrbSearchByDescription, gridBagConstraintsx02);

		GridBagConstraints gridBagConstraintsx13 = new GridBagConstraints();
		gridBagConstraintsx13.gridx = 0;
		gridBagConstraintsx13.gridy = 2;
		gridBagConstraintsx13.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx13.gridwidth = 1;
		gridBagConstraintsx13.fill = GridBagConstraints.WEST;
		container.add(jlbDummy, gridBagConstraintsx13);

		GridBagConstraints gridBagConstraintsx03 = new GridBagConstraints();
		gridBagConstraintsx03.gridx = 1;
		gridBagConstraintsx03.gridy = 2;
		gridBagConstraintsx03.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx03.gridwidth = 1;
		gridBagConstraintsx03.fill = GridBagConstraints.WEST;
		container.add(jlbSearchByDescription, gridBagConstraintsx03);

		GridBagConstraints gridBagConstraintsx04 = new GridBagConstraints();
		gridBagConstraintsx04.gridx = 2;
		gridBagConstraintsx04.gridy = 2;
		gridBagConstraintsx04.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx04.gridwidth = 1;
		gridBagConstraintsx04.gridheight = 1;
		gridBagConstraintsx04.fill = GridBagConstraints.BOTH;
		container.add(jtfSearchByDescription, gridBagConstraintsx04);

		GridBagConstraints gridBagConstraintsx05 = new GridBagConstraints();
		gridBagConstraintsx05.gridx = 0;
		gridBagConstraintsx05.gridy = 3;
		gridBagConstraintsx05.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx05.gridwidth = 3;
		gridBagConstraintsx05.fill = GridBagConstraints.BOTH;
		container.add(jrbStatus, gridBagConstraintsx05);

		GridBagConstraints gridBagConstraintsx06 = new GridBagConstraints();
		gridBagConstraintsx06.gridx = 1;
		gridBagConstraintsx06.gridy = 4;
		gridBagConstraintsx06.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx06.gridwidth = 1;
		gridBagConstraintsx06.fill = GridBagConstraints.BOTH;
		container.add(jlbStatus, gridBagConstraintsx06);

		GridBagConstraints gridBagConstraintsx07 = new GridBagConstraints();
		gridBagConstraintsx07.gridx = 2;
		gridBagConstraintsx07.gridy = 4;
		gridBagConstraintsx07.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx07.gridwidth = 1;
		gridBagConstraintsx07.fill = GridBagConstraints.BOTH;
		container.add(jcbStatus, gridBagConstraintsx07);

		GridBagConstraints gridBagConstraintsx10 = new GridBagConstraints();
		gridBagConstraintsx10.gridx = 1;
		gridBagConstraintsx10.gridy = 6;
		gridBagConstraintsx10.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx10.gridwidth = 1;
		gridBagConstraintsx10.fill = GridBagConstraints.BOTH;
		container.add(jbnSearch, gridBagConstraintsx10);

		GridBagConstraints gridBagConstraintsx11 = new GridBagConstraints();
		gridBagConstraintsx11.gridx = 2;
		gridBagConstraintsx11.gridy = 6;
		gridBagConstraintsx11.insets = new Insets(5, 5, 5, 5);
		gridBagConstraintsx11.gridwidth = 1;
		gridBagConstraintsx11.fill = GridBagConstraints.BOTH;
		container.add(jbnCancel, gridBagConstraintsx11);

		jrbSearchByDescription.setSelected(true);
		jrbStatus.setSelected(false);
		
		jtfSearchByDescription.setEnabled(true);
		jlbSearchByDescription.setEnabled(true);
		jtfSearchByDescription.setEnabled(false);

		jbnSearch.addActionListener(this);
		jbnCancel.addActionListener(this);
		jrbSearchByDescription.addActionListener(this);
		jrbStatus.addActionListener(this);
		
	
	}

	

	public void stateChanged(ChangeEvent arg0) {

	}

	public static SearchPage getInstance() {
		instance = new SearchPage();
		return instance;
	}
	public static SearchPage getNewInstance(User user) {
		instance = new SearchPage(user);
		return instance;
	}
/**
 * preiau event-uri
 * setez vizibilitatea radioButonn, JLabel
 */
	@Override
	public void actionPerformed(ActionEvent e) {

		try {
			if (e.getSource() == jbnSearch) {
				search();
			} else if (e.getSource() == jbnCancel) {
				cancel();
			} else if (e.getSource() == jrbSearchByDescription) {
				jtfSearchByDescription.setEnabled(true);
				jlbSearchByDescription.setEnabled(true);
				
				jrbStatus.setEnabled(true);
				jcbStatus.setEnabled(false);

			} else if (e.getSource() == jrbStatus) {
				jtfSearchByDescription.setEnabled(false);
				jlbSearchByDescription.setEnabled(false);
				jrbSearchByDescription.setEnabled(true);
				jcbStatus.setEnabled(true);
				
			}

		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	private void fillStatusComboBox() {
		jcbStatus.addItem("TO DO");
		jcbStatus.addItem("IN PROGRESS");
		jcbStatus.addItem("DONE");
		jcbStatus.addItem("CANCELED");
		
	}
/**
 * 
 */
	private void search() {
		try {
			String searchedTaskByDescription = jtfSearchByDescription.getText().trim();
			String searchedTaskByStatus = (String) jcbStatus.getSelectedItem();

			// TODO validari
			//boolean success=false;
			if (jrbSearchByDescription.isSelected()) {
				
				
				tasks=TasksManager.getInstance().searchByDescription(user,searchedTaskByDescription);
				
			
					
					ApplicationScreen.getInstance().setContainer(
						UserPage.getNewInstance(user,tasks).getContainer());
				
				
				
			} else {

				if (jrbStatus.isSelected()) {
					
					tasks=TasksManager.getInstance().searchByStatus(user,searchedTaskByStatus);
			
					ApplicationScreen.getInstance().setContainer(
							UserPage.getNewInstance(user,tasks).getContainer());
				
				
			}
			}
		} catch (Exception e) {
			JOptionPane.showMessageDialog(ApplicationScreen.getInstance()
					.getFrame(), "Introduceti corect datele necesare!",
					"Data Input Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	private void cancel() {
		ApplicationScreen.getInstance().setContainer(
				UserPage.getInstance().getContainer());
	}

	/**
	 * @return
	 */
	public Container getContainer() {
		return container;
	}
}
